package _6_泛型;

import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

public class _2_使用泛型 {

    @Test
    public void f(){
        List list = new ArrayList<>();
        list.add("String");
        String temp = (String)list.get(0);
        System.out.println(temp);
    }

    @Test
    public void f1(){
        List<String> list = new ArrayList<>();
        list.add("Ssss");
        String temp = list.get(0);
        System.out.println(temp);
    }

    @Test
    public void f2(){
        List<Number> list = new ArrayList<>();
        list.add(Integer.valueOf(1));
        list.add(Float.valueOf((float)2.0));
        Number number1 = list.get(0);
        Number number2 = list.get(1);
        System.out.println(number1);
        System.out.println(number2);
    }

    @Test
    public void f3(){
        String[] strings = new String[]{"A","C","B"};
        Arrays.sort(strings);
        System.out.println(Arrays.toString(strings));
    }

    class Person{
        String name;
        int score;
        public Person(String name,int score){
            this.name = name;
            this.score = score;
        }
        public String toString(){
            return this.name + "," + this.score;
        }
    }


    @Test
    public void f4_1(){
        Person[] persons = new Person[]{
                new Person("doug",21),
                new Person("jby",55),
                new Person("nobody",999)
        };
        Arrays.sort(persons);
        System.out.println(Arrays.toString(persons));
    }

    class Person1 implements Comparable<Person1>{
        String name;
        int score;
        public Person1(String name,int score){
            this.name = name;
            this.score = score;
        }
        public String toString(){
            return this.name + "," + this.score;
        }

        @Override
        public int compareTo(Person1 o) {
            return this.name.compareTo(o.name);
        }
    }

    @Test
    public void f4_2(){
        Person1[] persons = new Person1[]{
                new Person1("doug",21),
                new Person1("jby",55),
                new Person1("nobody",999)
        };
        Arrays.sort(persons);
        System.out.println(Arrays.toString(persons));
    }

    /**
     * 字符串排序 写法1
     */

    @Test
    public void f4_3_1(){

        Person[] persons = new Person[]{
                new Person("doug",21),
                new Person("jby",55),
                new Person("nobody",999)
        };
        Arrays.sort(persons,new Comparator<Person>(){
            public int compare(Person person1, Person person2) {
                return person1.name.compareTo(person2.name);
            }
        });
    }

    /**
     * 字符串排序 写法2
     */

    @Test
    public void f4_3_2(){
        Person[] persons = new Person[]{
                new Person("j",2),
                new Person("b",3),
                new Person("y",5)
        };
//        Arrays.sort(persons,(person1,person2)-> {
//            return person1.name.compareTo(person2.name);
//        });
        Arrays.sort(persons,(person1,person2)-> {
            return person1.name.compareTo(person2.name);
        });
        System.out.println(Arrays.toString(persons));
    }

    /**
     * 分数排序
     *
     *
     */

    class Person2 implements Comparable<Person2>{
        String name;
        int score;
        public Person2(String name,int score){
            this.name = name;
            this.score = score;
        }
        public String toString(){
            return this.name + "," + this.score;
        }

        @Override
        public int compareTo(Person2 o) {
            if(this.score > o.score){
                return 1;
            }
            else if (this.score == o.score){
                return 0;
            }
            else{
                return -1;
            }
        }
    }

    @Test
    public void f5(){
        Person2[] person2s = new Person2[]{
                new Person2("j",12),
                new Person2("b",23),
                new Person2("y",34)
        };

        Arrays.sort(person2s,Person2::compareTo);
        System.out.println(Arrays.toString(person2s));
    }

}
